package com.example.exammaster.controller;

import com.example.exammaster.pojo.WrongExam;
import com.example.exammaster.pojo.comment;
import com.example.exammaster.pojo.Exam;
import com.example.exammaster.pojo.Topic;
import com.example.exammaster.service.ExamService;
import com.example.exammaster.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/exam")
public class ExamController {

    @Resource
    private ExamService examService;


    @GetMapping("/get")
    public ResponseResult getById(Integer projectId, Integer examId, Integer projectSelectId){
        Exam exam = examService.getExamById(projectId, --examId, projectSelectId);
        if(exam!=null){
            return new ResponseResult(true,"查询成功",exam);
        }
        return new ResponseResult(false,"查询失败");
    }

    /**
     * 提交答案修改数据
     * @param examId
     * @param submit
     * @return
     */
    @RequestMapping("/update")
    public ResponseResult updateById(Integer examId,char submit){
        int row = examService.update(examId,submit);
        if(row>0){
            return new ResponseResult(true,"修改成功");
        }
        return new ResponseResult(false,"修改失败");
    }


    /**
     * 为错题添加时间字段
     */
    String date=new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    @RequestMapping("/insertWrong")
    public ResponseResult insertWrong(Integer projectId, Integer projectSelectId,Integer id){
        int row = examService.addWrong(projectId,projectSelectId,date,id);
        if(row>0){
            return new ResponseResult(true,"添加成功");
        }
        return new ResponseResult(false,"添加失败");
    }

    /**
     * 获取所有的错题时间列表
     * @param projectId
     * @return
     */
    @RequestMapping("/getWrongListById")
    public ResponseResult getWrongListById(Integer projectId, Integer projectSelectId){
        List<String> dateList = examService.getWrongListById(projectId,projectSelectId);
        if(dateList!=null){
            return new ResponseResult(true,"查询成功",dateList);
        }
        return new ResponseResult(false,"查询失败");
    }

    @RequestMapping("/getWrongListByIdDate")
    public ResponseResult getWrongListByIdDate(Integer projectId, String date, Integer projectSelectId){
        List<String> dateList = examService.getWrongListByIdDate(projectId,date,projectSelectId);
        if(dateList!=null){
            return new ResponseResult(true,"查询成功",dateList);
        }
        return new ResponseResult(false,"查询失败");
    }

    @RequestMapping("/getWrongQuestion")
    public ResponseResult getWrongQuestion(Integer projectId,String date,Integer id, Integer projectSelectId){
        Exam wrongQuestion = examService.getWrongQuestion(projectId,date,--id,projectSelectId);
        if(wrongQuestion!=null){
            return new ResponseResult(true,"查询成功",wrongQuestion);
        }
        return new ResponseResult(false,"查询失败");
    }

    @RequestMapping("/getProjectSelect")
    public ResponseResult getProjectSelectList(Integer projectId){
        List<String> list = examService.getProjectSelectList(projectId);
        if(list!=null){
            return new ResponseResult(true,"查询成功",list);
        }
        return new ResponseResult(false,"查询失败");
    }

    @RequestMapping("/getQuestionCount")
    public ResponseResult questionCount(Integer projectId, Integer projectSelectId){
        int count = examService.getQuestionCount(projectId,projectSelectId);
        if(count>0){
            return new ResponseResult(true,"查询成功",count);
        }
        return new ResponseResult(true,"查询失败");
    }

    @RequestMapping("/getWrongQuestionCount")
    public ResponseResult wrongQuestionCount(Integer projectId, Integer projectSelectId, String date){
        int count = examService.getWrongQuestionCount(projectId,projectSelectId,date);
        if(count>0){
            return new ResponseResult(true,"查询成功",count);
        }
        return new ResponseResult(true,"查询失败");
    }

    @RequestMapping("/setWrongRate")
    public ResponseResult setWrongRate(Integer projectId,Integer projectSelectId){
        int row = examService.setWrongRate(projectId,projectSelectId,date);
        if(row>0){
            return new ResponseResult(true,"插入成功",row);
        }
        return new ResponseResult(true,"插入失败");
    }

    @RequestMapping("/setQuestionSolvedCount")
    public ResponseResult setQuestionSolvedCount(Integer projectId,Integer projectSelectId){
        int count = examService.setQuestionCount(projectId,projectSelectId,date);  //更新题目解决数量
        if(count>0){
            return new ResponseResult(true,"插入成功",count);
        }
        return new ResponseResult(true,"插入失败");
    }

    @RequestMapping("/updateWrongQuestionSolvedCount")
    public ResponseResult updateQuestionSolvedWrongCount(Integer projectId,Integer projectSelectId){
        int count = examService.updateQuestionSolvedWrongCount(projectId,projectSelectId,date);
        if(count>0){
            return new ResponseResult(true,"修改成功");
        }
        return new ResponseResult(false,"修改失败");
    }

    @RequestMapping("/getWrongRate")
    public ResponseResult getWrongRate(Integer projectId,Integer projectSelectId){
        String count = examService.getQuestionSolvedRate(projectId,projectSelectId,date);
        if(count!=null){
            return new ResponseResult(true,"获取成功",count);
        }
        return new ResponseResult(false,"获取失败");
    }

    @RequestMapping("/getLineData")
    public ResponseResult getLineData(Integer projectId,Integer projectSelectId){
        List<String> list = examService.getLineData(projectId,projectSelectId);
        if(list!=null){
            return new ResponseResult(true,"获取成功",list);
        }
        return new ResponseResult(false,"获取失败");
    }

    @RequestMapping("/getLineDate")
    public List<String> getLineDateList(Integer projectId,Integer projectSelectId){
        List<String> list = examService.getLineDateList(projectId,projectSelectId);
        return list;
    }

    @RequestMapping("/getLineName")
    public String getLineName(Integer projectId,Integer projectSelectId){
        String name = examService.getLineName(projectId,projectSelectId);
        return name;
    }

    @RequestMapping("/updateChapterCount")
    public String addChapterCount(Integer projectId,Integer chapterId){
        int res = examService.addChapterCount(projectId,chapterId);
        if(res==1){
            return "ok";
        }
        return "fail";
    }

    @RequestMapping("/updateChapterWrongCount")
    public String addChapterWrongCount(Integer projectId,Integer chapterId){
        int res = examService.addChapterWrongCount(projectId,chapterId);
        if(res==1){
            return "ok";
        }
        return "fail";
    }

    @RequestMapping("/getChapterList")
    public List<String> getChapterList(Integer projectId,Integer projectSelectId){
        List<String> list = examService.getChapterList(projectId,projectSelectId);
        return list;
    }

    @RequestMapping("/getChapterRate")
    public double[] getChapterRate(Integer projectId,Integer projectSelectId){
        double[] arr = new double[50];
        arr = examService.getChapterRate(projectId,projectSelectId);
        return arr;
    }

    @RequestMapping("/getChapterQuestion")
    public Exam getChapterQuestion(Integer projectId,Integer projectSelectId,Integer chapterId,Integer questionId){
        Exam exam = examService.getChapterQuestion(projectId,projectSelectId,chapterId,--questionId);
        return exam;
    }

    @RequestMapping("/getChapterCount")
    public int getChapterCount(Integer projectId,Integer projectSelectId,Integer chapterId){
        int count = examService.getChapterCount(projectId,projectSelectId,chapterId);
        return count;
    }

    @RequestMapping("/getShortList")
    public List<String> getShortList(Integer projectId,Integer projectSelectId){
        List<String> list = examService.getShortList(projectId,projectSelectId);
        return list;
    }

    @RequestMapping("/getYearList")
    public List<String> getYearList(Integer projectId,Integer projectSelectId){
        List<String> list = examService.getYearList(projectId,projectSelectId);
        return list;
    }

    @RequestMapping("/getIntelligence")
    public List<Exam> getIntelligence(Integer projectId,Integer projectSelectId){
        List<Exam> list = new ArrayList<>();
        int[] examIdList = examService.getExamIdList(projectId,projectSelectId);
        System.out.println("------------------------------------");
        for(int j = 0;j<5;j++) {
            for (int i = 0; i < examIdList.length; i++) {
                int chapterId = examService.getChapterIdList(projectId, examIdList[i]);
                System.out.println(chapterId);
                if(examService.getChapterQuestion(projectId,projectSelectId,chapterId,j)!=null) {
                    list.add(examService.getChapterQuestion(projectId, projectSelectId, chapterId, j));
                }
            }
        }
        return list;
    }

    @RequestMapping("/getTopicList")
    public List<Topic> getTopicList(){
        List<Topic> list = new ArrayList<>();
        list = examService.getTopicList();
        return list;
    }

    @RequestMapping("/addTopic")
    public void addTopic(String title,String submitImg){
        String img = "https://thirdwx.qlogo.cn/mmopen/vi_32/KZd62NRVDNp76hwvkDR61ewDLnNFFfHQlP76h3MLJhBIBSNqAoliaEevaUed3cy93wicyicgv7nIFSLuywwM2wXgQ/132";
        examService.addTopic(img,title,date,submitImg);
    }

    @RequestMapping("/getTopic")
    public Topic getTopic(int id){
        return examService.getTopic(id);
    }


    @RequestMapping("/getStudyDay")
    public int getStudyDay(int projectId,int projectSelectId){
      int count = examService.getStudyDay(projectId,projectSelectId);
      return count;
    }

    @RequestMapping("/addComment")
    public void addComment(int topicId,String comment){
        String img = "https://thirdwx.qlogo.cn/mmopen/vi_32/KZd62NRVDNp76hwvkDR61ewDLnNFFfHQlP76h3MLJhBIBSNqAoliaEevaUed3cy93wicyicgv7nIFSLuywwM2wXgQ/132";
        examService.addComment(topicId,img,comment,date);
    }

    @RequestMapping("/getComment")
    public List<comment> getComment(int topicId){
        return examService.getComment(topicId);
    }

    @RequestMapping("/getCommentList")
    public List<comment> getComment(){
        return examService.getCommentList();
    }

    @RequestMapping("/addMathQuestion")
//    public void addMathQuestion(Exam exam){
    public void addMathQuestion(String title,String answerA,String answerB,String answerC,String answerD,String realAnswer,int projectSelectId,int chapterId,String examYear,String isImage){
        examService.addMathQuestion(title,answerA,answerB,answerC,answerD,realAnswer,projectSelectId,chapterId,examYear,isImage);
    }

    @RequestMapping("/getMathQuestion")
    public List<Exam> getMathQuestion(){
        return examService.getMathQuestion();
    }

    @RequestMapping("/selectMathQuestion")
    public List<Exam> selectMathQuestion(int projectSelectId, int chapterId){
        return examService.selectMathQuestion(projectSelectId,chapterId);
    }

    @RequestMapping("/getMathQuestionById")
    public Exam getMathQuestionById(int examId){
        return examService.getMathQuestionById(examId);
    }

    @RequestMapping("/updateMathQuestionById")
    public void updateMathQuestionById(String title, String answerA, String answerB, String answerC, String answerD, String realAnswer, int projectSelectId, String examYear, String isImage, int examId){
        examService.updateMathQuestionById(title,answerA,answerB,answerC,answerD,realAnswer,projectSelectId,examYear,isImage,examId);
    }

    @RequestMapping("/getPoliticalQuestion")
    public List<Exam> getPoliticalQuestion(){
        return examService.getPoliticalQuestion();
    }

    @RequestMapping("/getMajorQuestion")
    public List<Exam> getMajorQuestion(){
        return examService.getMajorQuestion();
    }

    @RequestMapping("/getMathWrongQuestion")
    public List<WrongExam> getMathWrongQuestion(){
        return examService.getMathWrongQuestion();
    }

    @RequestMapping("/getPoliticalWrongQuestion")
    public List<WrongExam> getPoliticalWrongQuestion(){
        return examService.getPoliticalWrongQuestion();
    }

    @RequestMapping("/getMajorWrongQuestion")
    public List<WrongExam> getMajorWrongQuestion(){
        return examService.getMajorWrongQuestion();
    }

}
